package com.jeremyfeinstein.utils;

import android.app.Application;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;

import com.jeremyfeinstein.okhttputil.OkHttpUtils;

import org.apache.commons.codec.binary.Base64;

import java.io.IOException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;

public class ConferenceApp extends MultiDexApplication {

    private static final String TAG = ConferenceApp.class.getSimpleName();
    private static Application mContext;

    public static Application getApp() {
        return mContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        SPUtil.init(this);
        MultiDex.install(this);
        OkHttpUtils.initOkHttpClient(getApplicationContext());
        userCert();
        YCHSInstance.getInstance();
        initRSA();
    }

    public static KeyPair kp;

    private void initRSA() {
        try {
            kp = RSAUtils.generateKeyPair();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public static byte[] getPubK() {
        return Base64.encodeBase64(kp.getPublic().getEncoded());
    }

    public static byte[] getPriK() {
        return Base64.encodeBase64(kp.getPrivate().getEncoded());
    }

    private void userCert() {
        try {
            OkHttpUtils.getInstance().setCertificates(getAssets().open("ca.crt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
